﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.Composition;
using UnfriendlyFire.Models;
using System.Web.UI;

namespace UnfriendlyFire.Controllers
{
    [Export(typeof(IController))]
    [ExportMetadata("Name", "Theater")]
    [PartCreationPolicy(System.ComponentModel.Composition.CreationPolicy.NonShared)]
    public class TheaterController : Controller
    {
        private GameRepository _gameRepository;

        [ImportingConstructor]
        public TheaterController(GameRepository gameRepository)
        {
            _gameRepository = gameRepository;
        }

        public ActionResult Index()
        {
            return View();
        }

        [OutputCache(NoStore = true, Location = OutputCacheLocation.None)]
        public ActionResult IndexData()
        {
            var theaters = _gameRepository.Theaters.Select(
                t => new
                {
                    name = t.Name,
                    description = t.Description,
                    latitude = t.Latitude,
                    longitude = t.Longitude,
                    author = t.Author.Email
                });
            return Json(theaters.ToList(), JsonRequestBehavior.AllowGet);
        }

        public ActionResult Details(int id)
        {
            return View();
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [Authorize]
        public ActionResult Create(Theater theater)
        {
            try
            {
                var officer = _gameRepository.Officers.First(o => o.Email == HttpContext.User.Identity.Name);
                theater.Author = officer;
                _gameRepository.Theaters.AddObject(theater);
                _gameRepository.SaveChanges();
                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                return View();
            }
        }

        public ActionResult Edit(int id)
        {
            return View();
        }

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Delete(int id)
        {
            return View();
        }

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
